﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace AntikCompta
{
    class Tools
    {
        public static DataGridView Copy_DataGridView(DataGridView aDataGridView)
        {
            DataGridView dgp = new DataGridView();

            foreach (DataGridViewColumn col in aDataGridView.Columns)
            {
                dgp.Columns.Add((DataGridViewColumn)col.Clone());
            }

            int idxRow = 0;
            foreach (DataGridViewRow row in aDataGridView.Rows)
            {
                idxRow = dgp.Rows.Add();

                int idxCell = 0;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dgp.Rows[idxRow].Cells[idxCell].Value = cell.Value;
                    idxCell++;
                }
            }

            return dgp;
        }

        public static int AddTotaux(DataGridView aDataGridView, int aIdxStart, int aIdxEnd)
        {
            int idx = aDataGridView.Rows.Add();
            aDataGridView.Rows[idx].Cells[0].Value = "Totaux";

            for (int i = aIdxStart; i <= aIdxEnd; i++)
            {
                float val = 0;
                foreach (DataGridViewRow row in aDataGridView.Rows)
                {
                    if (row.Cells[i].Value != null)
                        val += Convert.ToSingle(row.Cells[i].Value);
                }

                aDataGridView.Rows[idx].Cells[i].Value = Math.Round(val, 2);
            }

            return idx;

        }

        public static bool GetConfig(String aKey, out String aValue)
        {
            aValue = "";
            if (Properties.Settings.Default[aKey] == null)
                return false;

            aValue = Properties.Settings.Default[aKey].ToString();

            return true;
        }

        public static bool SetConfig(String aKey, String aValue)
        {
            Properties.Settings.Default[aKey] = aValue;

            Properties.Settings.Default.Save();

            return true;
        }
    }
}
